<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul,
        ol,
        li {
            list-style: none;
        }
        
        .wp {
            width: 500px;
            height: 300px;
            border: 1px solid #ccc;
            overflow: hidden;
            margin: 100px;
            position: relative;
        }
        
        ul {
            width: 5000px;
            height: 100%;
        }
        
        ul>li {
            float: left;
            height: 100%;
            width: 500px;
            font-size: 100px;
            line-height: 300px;
            text-align: center;
            font-weight: bold;
        }
        
        ul>li.one {
            background: red;
        }
        
        ul>li.two {
            background: yellow;
        }
        
        ul>li.three {
            background: orange;
        }
        
        ul>li.four {
            background: pink;
        }
        
        ul>li.five {
            background: yellowgreen;
        }
        
        ol {
            position: absolute;
            bottom: 20px;
            left: 50%;
            margin-left: -75px;
        }
        
        ol>li {
            float: left;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            margin-right: 10px;
            background-color: purple;
        }
        
        .nav {
            position: absolute;
            left: 0;
            top: 50%;
            margin-top: -20px;
            height: 40px;
            width: 40px;
            background-color: brown;
        }
        
        .nav.right {
            left: auto;
            right: 0;
        }
        
        .on {
            background-color: #999;
        }
    </style>
</head>

<body>
    <div class="wp">
        <ul>
            <li class="one">1</li>
            <li class="two">2</li>
            <li class="three">3</li>
            <li class="four">4</li>
            <li class="five">5</li>
        </ul>
        <ol>
            <li class="on"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
        <div class="nav left"></div>
        <div class="nav right"></div>
    </div>

    <script>
        let leftBtn = document.querySelector(".left");
        let rightBtn = document.querySelector(".right");
        let ul = document.querySelector("ul");
        let pics = document.querySelectorAll("ul>li");
        let picWidth = document.querySelector("ul>li").clientWidth;
        let circles = document.querySelectorAll("ol>li");
        let num = 0;
        let timer;
        let flag = true;


        leftBtn.onclick = function() {
            if (timer) {
                clearInterval(timer)
            }
            let firstPic = document.querySelector("ul>li:first-child");
            let lastPic = document.querySelector("ul>li:last-child");
            ul.insertBefore(lastPic, firstPic);
            num = -picWidth;
            ul.style.marginLeft = num + "px";
            timer = setInterval(function() {
                num += 10
                if (num >= 0) {
                    clearInterval(timer)
                    getClass()
                }
                ul.style.marginLeft = num + "px";
            }, 10)
        }

        rightBtn.onclick = function() {
            // if (timer) {
            //     clearInterval(timer)
            // }
            timer = setInterval(function() {
                num -= 10;
                if (num <= -picWidth) {
                    clearInterval(timer);
                    ul.appendChild(document.querySelector("ul>li:first-child"))
                    num = 0;
                    getClass()
                }
                ul.style.marginLeft = num + "px";
            }, 30)
        }

        //给每一张图片添加一个index属性
        pics.forEach((item, index) => {
            //设置index属性
            item.setAttribute("index", index)
        })

        //小圆点点击事件
        circles.forEach((item, index) => {
            item.onclick = function() {
                if (!flag) {
                    return;
                }
                flag = false;
                //当前图片的索引值
                let oldIndex = document.querySelector("ul>li:first-child").getAttribute("index");
                //点击按钮的索引值
                let newIndex = index;
                //克隆的是当前显示的第一张
                let tmpItem = document.querySelector('ul>li:first-child').cloneNode(true)

                circles.forEach(item => {
                    item.className = ""
                })

                this.className = "on"
                if (newIndex > oldIndex) {
                    //把几张图片追加到后面
                    for (var i = 0; i < newIndex - oldIndex; i++) {
                        ul.appendChild(document.querySelector("ul>li:first-child"))
                    }
                    //把克隆好的图片放在当前第一张图片之前
                    ul.insertBefore(tmpItem, document.querySelector("ul>li:first-child"))
                    timer = setInterval(function() {
                        num -= 10;
                        if (num <= -picWidth) {
                            clearInterval(timer);
                            document.querySelector("ul>li:first-child").remove()
                            num = 0;
                            flag = true;
                        }
                        ul.style.marginLeft = num + "px";
                    }, 30)

                } else if (newIndex < oldIndex) {
                    for (var i = 0; i < oldIndex - newIndex; i++) {
                        ul.insertBefore(document.querySelector("ul>li:last-child"), document.querySelector("ul>li:first-child"))
                    }
                    document.querySelector("ul>li:first-child").after(tmpItem)
                    num = -picWidth;
                    ul.style.marginLeft = -picWidth + "px";
                    timer = setInterval(function() {
                        num += 10;
                        if (num >= 0) {
                            clearInterval(timer);
                            document.querySelector('ul>li:first-child').nextElementSibling.remove();
                            flag = true;
                        }
                        ul.style.marginLeft = num + "px";
                    }, 30)
                }

            }
        })

        function getClass() {
            let picIndex = document.querySelector("ul>li:first-child").getAttribute("index");
            circles.forEach((item, index) => {
                if (picIndex == index) {
                    item.className = "on"
                } else {
                    item.className = ""
                }
            })
        }

        // timer = setInterval(function() {
        //     rightBtn.onclick()
        // }, 2000)
    </script>



</body>

</html>